package ee.android.kinokavad.adapters;

import java.util.ArrayList;
import java.util.List;

import ee.android.kinokavad.R;
import ee.android.kinokavad.data.Session;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class SessionAdapter extends ArrayAdapter<Session> {

	private List<Session> sessions;
	private Context context;
	public SessionAdapter(Context context, int textViewResourceId, List<Session> list) {
		super(context, textViewResourceId, list);
		this.sessions = list;
		this.context = context;
	}
	
	@Override
    public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.session_item, null);
            }
            if (sessions.size()>0){
            	Session session = sessions.get(position);
            	if (session != null) {
            		
            		
            			TextView timeTV = (TextView) v.findViewById(R.id.timeTextView);
                		TextView movieName = (TextView) v.findViewById(R.id.movieNameTextView);
                		if (timeTV != null) {
                			String time = session.getTime();
                			if (time.length()>5){
                				time=time.substring(0,5);
                			}
                			timeTV.setText(time);
                		}
                		if(movieName != null && session.getMovie() != null){
                			movieName.setText(session.getMovie().getTitle());
            		}
            	}
            }
            return v;
	}

}
